const path = require('path');
import * as Koa from 'koa';
import getConnection from "../mysql/connection";
import { generateSql } from "./sql";
import { dealwithMySQLDate } from '../utils/common';

/**
 * 增加用户
 * @param {*} user 
 */
export async function addUser(ctx: Koa.Context, data: any)  {
  let connection: any = null;
  try {
    if (!data) return { success: false };
    const db = ctx.dbPool;
    connection = await getConnection(db);
    const sql = generateSql("user", "insert", data);
    if (!sql) return { msg: "生成sql语句错误", success: false };
    await connection.execute(sql, Object.values(data));
    return { success: true };
  } catch (error) {
    return { msg: error.sqlMessage || "", success: false };
  } finally {
    if (connection) connection.release();
  }
}

/**
 * 删除用户
 * @param {*} id 用户ID
 */
export async function delUser(ctx: Koa.Context, params: Object) {
  let connection: any = null;
  try {
    const db = ctx.dbPool;
    connection = await getConnection(db);
    const sql = generateSql("user", "delete", params);
    if (!sql) return { success: false };
    await connection.execute(sql);
    return { success: true };
  } catch (error) {
    return { msg: error.sqlMessage || "", success: false };
  } finally {
    if (connection) connection.release();
  }
}

/**
 * 更新指定用户
 * @param {*} user 
 */
export async function updateUser(ctx: Koa.Context, data: any)  {
  let connection: any = null;
  try {
    if (!data?.uid) return { msg: "用户ID不能为空", success: false };
    const db = ctx.dbPool;
    connection = await getConnection(db);
    const sql = generateSql("user", "update", data);
    if (!sql) return { success: false };
    await connection.execute(sql, Object.values(data));
    return { success: true };
  } catch (error) {
    return { msg: error.sqlMessage || "", success: false };
  } finally {
    if (connection) connection.release();
  }
}

/**
 * 查询
 * @param {*} params 查询条件
 */
export async function searchUser(ctx: Koa.Context, params: Object) {
  let connection: any = null;
  try {
    const db = ctx.dbPool;
    connection = await getConnection(db);
    const sql = generateSql("user", "query", params);
    if (!sql) return { success: false };
    const [result] = await connection.execute(sql);
    return { success: true, data: dealwithMySQLDate(result, { birthday: "date" }) };
  } catch (error) {
    return { msg: error.sqlMessage || "", success: false };
  } finally {
    if (connection) connection.release();
  }
}
export async function searchUserTotalNumbers(ctx: Koa.Context) {
  let connection: any = null;
  try {
    const db = ctx.dbPool;
    connection = await getConnection(db);
    const [result] = await connection.execute("SELECT COUNT(*) FROM user");
    return { success: true, data: result?.[0]?.["COUNT(*)"] || 0 };
  } catch (error) {
    return { msg: error.sqlMessage || "", success: false };
  } finally {
    if (connection) connection.release();
  }
}
